define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'volunteerbuf/index' + location.search,
                    add_url: 'volunteerbuf/add',
                    // edit_url: 'volunteerbuf/edit',
                    // del_url: 'volunteerbuf/del',
                    multi_url: 'volunteerbuf/multi',
                    import_url: 'volunteerbuf/import',
                    table: 'volunteerbuf',
                }
            });

            var table = $("#table");
            //当内容渲染完成给编辑按钮添加`data-area`属性
            table.on('post-body.bs.table', function (e, settings, json, xhr) {
                $(".c_user_record").data("area", ["100%", "100%"]);
                $(".t_user_record").data("area", ["100%", "100%"]);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormTemplate: 'customformtpl',
                searchFormVisible: true,
                search:false,
                showColumns:false,
                // showExport:false,
                // commonSearch:false,
                pageSize: 10,
                showToggle: false,
                exportOptions: {
                    fileName: '義工列表_' + Moment().format("YYYY-MM-DD"),  //导出名称
                    ignoreColumn: ['operate'], //不导出字段
                    ignoreRow: [],
                    mso:{
                        // fileFormat:        'xlsx',
                        //修复导出数字不显示为科学计数法
                        onMsoNumberFormat: function (cell, row, col) {
                            return !isNaN($(cell).text())?'\\@':'';
                        }
                    },
                    csvSeparator: "----",//文本分割
                },
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'user_id', title: __('User_id')},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'region_address', title: __('Region_address'), operate: false},
                        {field: 'address', title: __('Address'), operate: 'LIKE'},
                        {field: 'volunteerspecialty.name', title: __('Volunteerspecialty.name'), operate: 'LIKE'},
                        {field: 'service_time_type', title: __('Service_time_type'), searchList: {"1":__('Service_time_type 1'),"2":__('Service_time_type 2')}, formatter: Table.api.formatter.normal},
                        {field: 'staff.nickname', title: __('Staff.nickname'), operate: 'LIKE'},
                        {field: 'user.status', title: __('User.status'), formatter: Table.api.formatter.status, searchList: {normal: __('Normal'), hidden: __('Hidden')}},
                        {field: 'createtime', title: __('Createtime'), operate:false, addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},{field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter:function(value,row,index){

                                    this.buttons = [
                                        {
                                            name: 'refuse',
                                            text: '禁用',
                                            classname: 'btn btn-xs btn-warning btn-magic btn-ajax',
                                            url: 'volunteerbuf/updateuser',
                                            confirm: '确认禁用嗎？',
                                            visible: function (row) {
                                                if ( Config.updateuser && row.user.status == 'normal'  ) {
                                                    return true;
                                                }else{
                                                    return false;
                                                }
                                                //返回true时按钮显示,返回false隐藏
                                            },
                                            success: function () {
                                                $(".btn-refresh").trigger("click");
                                            },
                                            error: function () {

                                            },
                                            refresh: true,
                                        },{
                                            name: 'agree',
                                            text: '啟用',
                                            classname: 'btn btn-xs btn-success btn-ajax',
                                            url: 'volunteerbuf/updateuser',
                                            confirm: '确认啟用嗎？',
                                            visible: function (row) {
                                                if ( Config.updateuser && row.user.status == 'hidden' ) {
                                                    return true;
                                                }else{
                                                    return false;
                                                }
                                                //返回true时按钮显示,返回false隐藏
                                            },
                                            success: function () {
                                                $(".btn-refresh").trigger("click");
                                            },
                                            error: function () {

                                            },
                                            refresh: true,
                                        },{
                                            name: 'c_user_record',
                                            text: '任務記錄',
                                            classname: 'btn btn-xs btn-success btn-magic btn-dialog c_user_record',
                                            url: 'caring_task/user_record',
                                            visible: function (row) {
                                                if ( Config.c_user_record ) {
                                                    return true;
                                                }else{
                                                    return false;
                                                }
                                                //返回true时按钮显示,返回false隐藏
                                            },
                                            success: function () {
                                                $(".btn-refresh").trigger("click");
                                            },
                                            error: function () {

                                            },
                                            refresh: true,
                                        },{
                                            name: 't_user_record',
                                            text: '培訓記錄',
                                            classname: 'btn btn-xs btn-success btn-magic btn-dialog t_user_record',
                                            url: 'training_tasks/user_record',
                                            visible: function (row) {
                                                if ( Config.t_user_record ) {
                                                    return true;
                                                }else{
                                                    return false;
                                                }
                                                //返回true时按钮显示,返回false隐藏
                                            },
                                            success: function () {
                                                $(".btn-refresh").trigger("click");
                                            },
                                            error: function () {

                                            },
                                            refresh: true,
                                        }
                                    ];
                                return Table.api.formatter.operate.call(this,value,row,index);
                            }}
                    ]
                ]
            });

            table.on("pre-body.bs.table",function(){
                var bt=table.data("bootstrap.table");
                if (bt){
                    bt.$toolbar.find(".export").find(".icon-share").text("導出");
                    bt.$toolbar.find(".export").find(".icon-share").removeClass("glyphicon glyphicon-export icon-share");
                }
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        indexs: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'volunteerbuf/indexs/staff_id/'+Config.staff_id + location.search,
                    add_url: 'volunteerbuf/add',
                    edit_url: 'volunteerbuf/edit',
                    del_url: 'volunteerbuf/del',
                    multi_url: 'volunteerbuf/multi',
                    import_url: 'volunteerbuf/import',
                    table: 'volunteerbuf',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: true,
                search:false,
                showColumns:false,
                showExport:false,
                // commonSearch:false,
                pageSize: 10,
                showToggle: false,
                maintainSelected: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate:false},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'volunteerbufspecialty.name', title: __('volunteerbufspecialty.name'), operate: false},
                        {field: 'service_time_type', title: __('Service_time_type'), operate: false, searchList: {"1":__('Service_time_type 1'),"2":__('Service_time_type 2')}, formatter: Table.api.formatter.normal},
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            table.on('load-success.bs.table', function () {
                // 默認選中
                $("#table").bootstrapTable("checkBy", {field:"id", values:Config.arr})
            });
            // 获取选中项
            $(document).on("click", ".btn-selected", function () {
                // data = JSON.stringify(Table.api.selecteddata(table));
                data = Table.api.selecteddata(table);
                // Layer.alert();
                Fast.api.close(data);//这里是重点
            });
        },
        indexss: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'volunteerbuf/indexss/ids/'+Config.ids + location.search,
                    add_url: 'volunteerbuf/add',
                    edit_url: 'volunteerbuf/edit',
                    del_url: 'volunteerbuf/del',
                    multi_url: 'volunteerbuf/multi',
                    import_url: 'volunteerbuf/import',
                    table: 'volunteerbuf',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: true,
                search:false,
                showColumns:false,
                showExport:false,
                // commonSearch:false,
                pageSize: 10,
                showToggle: false,
                maintainSelected: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate:false},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'volunteerbufspecialty.name', title: __('volunteerbufspecialty.name'), operate: false},
                        {field: 'service_time_type', title: __('Service_time_type'), operate: false, searchList: {"1":__('Service_time_type 1'),"2":__('Service_time_type 2')}, formatter: Table.api.formatter.normal},
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            table.on('load-success.bs.table', function () {
                // 默認選中
                $("#table").bootstrapTable("checkBy", {field:"id", values:Config.arr})
            });
            // 启动和暂停按钮
            $(document).on("click", ".btn-start", function () {
                //在table外不可以使用添加.btn-change的方法
                //只能自己调用Table.api.multi实现
                //如果操作全部则ids可以置为空
                var ids = Table.api.selectedids(table);
                Fast.api.ajax({
                 url:$('.btn-start').attr('data-url'),
                 data:{volunteerbuf_ids:ids.join(","),action:'select'},
                loading:false,
                 success: function (data, ret) {
                    if ( data.code == 1 ) {
                        Toastr.success(data.msg);
                        setTimeout('Fast.api.close()', 1500);
                    }else{

                      Toastr.error(data.msg);
                     //这里ret.msg 是未定义的。 data里的数据需要 data.data.*来使用
                    }
                 }
                 });
                // Table.api.multi("", ids.join(","), table, this);
                // Fast.api.close();//这里是重点



                // Layer.close('操作成功');
            });

        },
        staff_volunteerbufs: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'volunteerbuf/staff_volunteerbufs/ids/'+Config.ids + location.search,
                    add_url: 'volunteerbuf/add',
                    edit_url: 'volunteerbuf/edit',
                    del_url: 'volunteerbuf/del',
                    multi_url: 'volunteerbuf/multi',
                    import_url: 'volunteerbuf/import',
                    table: 'volunteerbuf',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: true,
                search:false,
                showColumns:false,
                showExport:false,
                // commonSearch:false,
                pageSize: 10,
                showToggle: false,
                maintainSelected: true,
                columns: [
                    [
                        {field: 'id', title: __('Id'), operate:false},
                        {field: 'user.avatar', title: __('User.avatar'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'user.nickname', title: __('User.nickname'), operate: 'LIKE'},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'volunteerbufspecialty.name', title: __('volunteerbufspecialty.name'), operate: false},
                        {field: 'service_time_type', title: __('Service_time_type'), operate: false, searchList: {"1":__('Service_time_type 1'),"2":__('Service_time_type 2')}, formatter: Table.api.formatter.normal},
                        {field: 'region_address', title: __('Region_address'), operate: false},
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            table.on('load-success.bs.table', function () {
                // 默認選中
                $("#table").bootstrapTable("checkBy", {field:"id", values:Config.arr})
            });
            // 获取选中项
            $(document).on("click", ".btn-selected", function () {
                // data = JSON.stringify(Table.api.selecteddata(table));
                data = Table.api.selecteddata(table);
                // Layer.alert();
                Fast.api.close(data);//这里是重点
            });
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'volunteerbuf/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'volunteerbuf/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'volunteerbuf/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});